<?php

namespace yiike\goods\model;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yiike\goods\model\Goods;

/**
 * GoodsSearch represents the model behind the search form about `yiike\goods\model\Goods`.
 */
class GoodsSearch extends Goods
{
    /**
     * @inheritdoc
     */


    public function rules()
    {
        return [
            [['id', 'weid', 'type', 'top', 'orderby', 'cid', 'ccid', 'store_id', 'sold', 'full_num_shipping', 'invoice', 'status', 'sale_time_start', 'sale_time_end', 'hide_search', 'no_return', 'weight', 'stock', 'stock_rule', 'is_spec', 'max_buy', 'min_buy', 'quan_rule', 'give_credit1', 'created_by', 'updated_by', 'created_at', 'updated_at'], 'integer'],
            [['title', 'sub_title', 'short_title', 'keyword', 'tag', 'logo', 'image', 'no_shipping', 'location_p', 'location_c', 'goods_sn', 'property', 'content', 'share_title', 'share_image', 'share_content'], 'safe'],
            [['price', 'orig_price', 'cost_price', 'full_money_shipping', 'shipping_fee', 'commision'], 'number'],

        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
// bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {

        $query = Goods::find();


        $query->orderBy(['id' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'weid' => $this->weid,
            'type' => $this->type,
            'top' => $this->top,
            'orderby' => $this->orderby,
            'cid' => $this->cid,
            'ccid' => $this->ccid,
            'store_id' => $this->store_id,
            'price' => $this->price,
            'orig_price' => $this->orig_price,
            'cost_price' => $this->cost_price,
            'sold' => $this->sold,
            'full_num_shipping' => $this->full_num_shipping,
            'full_money_shipping' => $this->full_money_shipping,
            'shipping_fee' => $this->shipping_fee,
            'invoice' => $this->invoice,
            'status' => $this->status,
            'sale_time_start' => $this->sale_time_start,
            'sale_time_end' => $this->sale_time_end,
            'hide_search' => $this->hide_search,
            'no_return' => $this->no_return,
            'weight' => $this->weight,
            'stock' => $this->stock,
            'stock_rule' => $this->stock_rule,
            'is_spec' => $this->is_spec,
            'max_buy' => $this->max_buy,
            'min_buy' => $this->min_buy,
            'quan_rule' => $this->quan_rule,
            'give_credit1' => $this->give_credit1,
            'commision' => $this->commision,
            'created_by' => $this->created_by,
            'updated_by' => $this->updated_by,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'sub_title', $this->sub_title])
            ->andFilterWhere(['like', 'short_title', $this->short_title])
            ->andFilterWhere(['like', 'keyword', $this->keyword])
            ->andFilterWhere(['like', 'tag', $this->tag])
            ->andFilterWhere(['like', 'logo', $this->logo])
            ->andFilterWhere(['like', 'image', $this->image])
            ->andFilterWhere(['like', 'no_shipping', $this->no_shipping])
            ->andFilterWhere(['like', 'location_p', $this->location_p])
            ->andFilterWhere(['like', 'location_c', $this->location_c])
            ->andFilterWhere(['like', 'goods_sn', $this->goods_sn])
            ->andFilterWhere(['like', 'property', $this->property])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'share_title', $this->share_title])
            ->andFilterWhere(['like', 'share_image', $this->share_image])
            ->andFilterWhere(['like', 'share_content', $this->share_content]);

        return $dataProvider;
    }
}